<template>
  <div class="show-main">
    <el-scrollbar style="height: 100%;">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-button type="success" @click.stop="search">刷新</el-button>
          <el-button type="primary" @click.stop="add">添加</el-button>
        </div>
        <el-table :data="table" style="width: 400px;" border>
          <el-table-column prop="value" label="时间段"></el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="edit(scope.row)" size="small">编辑</el-button>
              <el-button type="text" @click="del(scope.row.id)" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog title="添加时间段" :visible.sync="addVisible" :before-close="handleClose('addForm')">
        <el-form ref="addForm" :model="addForm" label-width="80px" :rules="rules">
          <el-form-item label="起始时间" prop="start">
            <el-time-select
              placeholder="起始时间"
              v-model="addForm.start"
              :picker-options="{
                start: '06:00',
                step: '00:15',
                end: '23:30',
                maxTime: addForm.end
              }"
            >
            </el-time-select>
          </el-form-item>
          <el-form-item label="结束时间" prop="end">
            <el-time-select
              placeholder="结束时间"
              v-model="addForm.end"
              :picker-options="{
                start: '06:00',
                step: '00:15',
                end: '23:30',
                minTime: addForm.start
              }"
            >
            </el-time-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('addVisible', 'addForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('addVisible', 'addForm')" size="small">添 加</el-button>
        </span>
      </el-dialog>
      <el-dialog title="编辑时间段" :visible.sync="editVisible" :before-close="handleClose('editForm')">
        <el-form ref="editForm" :model="editForm" label-width="80px" :rules="rules">
          <el-form-item label="起始时间" prop="start">
            <el-time-select
              placeholder="起始时间"
              v-model="editForm.start"
              :picker-options="{
                start: '06:00',
                step: '00:15',
                end: '23:30',
                maxTime: editForm.end
              }"
            >
            </el-time-select>
          </el-form-item>
          <el-form-item label="结束时间" prop="end">
            <el-time-select
              placeholder="结束时间"
              v-model="editForm.end"
              :picker-options="{
                start: '06:00',
                step: '00:15',
                end: '23:30',
                minTime: editForm.start
              }"
            >
            </el-time-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('editVisible', 'editForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('editVisible', 'editForm')" size="small">编 辑</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import { getOrderTime, editOrderTime, addOrderTime, delOrderTime } from '@/network/api.js'

export default {
  name: 'ordertime',
  data() {
    return {
      table: [],
      addVisible: false,
      addForm: {
        start: '',
        end: ''
      },
      editVisible: false,
      editForm: {
        id: 0,
        start: '',
        end: ''
      },
      rules: {
        start: [{ required: true, message: '开始时间必须', trigger: 'blur' }],
        end: [{ required: true, message: '结束时间必须', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.search()
  },
  computed: {},
  methods: {
    async search() {
      try {
        const rtn = await getOrderTime({})
        this.table = rtn
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加
    add() {
      this.addVisible = true
    },
    // 删除
    del(id) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await delOrderTime({ id })
            this.search()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {
          this.$message.warning('您已取消操作')
        })
    },
    // 编辑
    edit(row) {
      this.editVisible = true
      this.$nextTick(() => {
        this.editForm.id = row.id
        this.editForm.start = row.value.split('-')[0]
        this.editForm.end = row.value.split('-')[1]
      })
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose(formName) {
      return (done) => {
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close(visibleName, formName) {
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send(visibleName, formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          switch (formName) {
            case 'editForm': // 编辑下单时间段
              try {
                await editOrderTime(this.editForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'addForm': // 添加下单时间段
              try {
                await addOrderTime(this.addForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          this.$message.error('请检查表单')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
